package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.ying.space.core.dao.PickGoodsChannelDAO;
import com.ecoolex.ying.space.entity.PickGoodsChannel;
import com.ecoolex.ying.space.entity.PickGoodsChannel.Column;
import com.ecoolex.ying.space.entity.PickGoodsChannelExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.Date;
import java.util.List;

@Service
public class PickGoodsChannelManager {
    @Autowired
    private PickGoodsChannelDAO dao;

    void deleteByPickGoodsId(Integer pickGoodsId, String updateUser) {
        PickGoodsChannel record = new PickGoodsChannel();
        record.setDeleted(PickGoodsChannel.IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        PickGoodsChannelExample example = new PickGoodsChannelExample();
        example.createCriteria().andPickGoodsIdEqualTo(pickGoodsId).andDeletedEqualTo(PickGoodsChannel.NOT_DELETED);
        dao.updateByExampleSelective(record,
                example,
                Column.deleted,
                Column.updateUser,
                Column.updateTime);
    }

    public PickGoodsChannel create(PickGoodsChannel record, String createUser) {
        record.setDeleted(PickGoodsChannel.NOT_DELETED);
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.insert(record);
        return record;
    }

    @Transactional
    public void update(PickGoodsChannel record, String updateUser) {
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        int row = dao.updateByPrimaryKeySelective(record,
                Column.name,
                Column.price,
                Column.contact,
                Column.updateUser,
                Column.updateTime);

        BizAssert.isTrue(row == 1, "修改失败");
    }

    @Transactional
    public void delete(PickGoodsChannel record, String updateUser) {
        record.setDeleted(PickGoodsChannel.IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        int row = dao.updateByPrimaryKeySelective(record,
                Column.deleted,
                Column.updateUser,
                Column.updateTime);

        BizAssert.isTrue(row == 1, "删除失败");
    }

    public PickGoodsChannel getById(Integer channelId) {
        return dao.selectByPrimaryKeyWithLogicalDelete(channelId, PickGoodsChannel.NOT_DELETED);
    }

    public List<PickGoodsChannel> queryByPickGoodsId(Integer pickGoodsId) {
        PickGoodsChannelExample example = new PickGoodsChannelExample();
        example.createCriteria().andPickGoodsIdEqualTo(pickGoodsId).andDeletedEqualTo(PickGoodsChannel.NOT_DELETED);
        return dao.selectByExample(example);
    }
}